/**
 * @(#)ContentServiceImpl.java 2010-2-24
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.core.services.impl;

import org.jbpm.api.Execution;
import org.jbpm.api.cmd.Environment;
import org.jbpm.api.model.OpenExecution;
import org.jbpm.pvm.internal.script.ScriptManager;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.core.services.api.IContentService;

/**
 * @author Administrator Email:makubexnet@gmail.com
 * @version 1.0.0
 */
public class ContentServiceImpl implements IContentService {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 4736895784316315270L;

	private Environment environment;

	/**
	 * @see com.wilmar.process.core.services.api.IContentService#resolveContent(java.lang.String,
	 *      org.jbpm.api.model.OpenExecution)
	 */
	public String resolveContent(String source, OpenExecution openExecution) {
		return this.evaluateExpression(source, openExecution);
	}

	private String evaluateExpression(String expression, Execution execution) {
		ScriptManager scriptManager = this.environment.get(ScriptManager.class);
		Object value = scriptManager.evaluateExpression(expression,
				ProcessSystemDeclareKeys.DEFAULT_SYSTEM_SCRIPT_LANGUAGE);
		if (!(value instanceof String)) {
			value = value.toString();
		}
		return (String) value;
	}

	/**
	 * @return the environment
	 */
	public Environment getEnvironment() {
		return environment;
	}

	/**
	 * @param environment
	 *            the environment to set
	 */
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

}
